package com.yueyi.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.yueyi.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "9021000137699784";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCbIpNPUMTmfqzIFcHfbpsOqo+Mj1jf18I8wAVWTXO8mzDv3H41LGo6SZxp/RVo8Pk9YB5mN1i0AX+7apV9ucccPunhF6D9dOGrIefFt/PCqjWhQ12wBq0RmoBqaXq2nultwLzTfW0zUn8nvyCSzepIURVBP2y+CoXLHsapHH4sHKdjNCRKWVsOMjFbC9FnK/H+Q5cAv1Ja1/KHZGKqIJaEUkfmdtPynHM1eOafUHCCfZ3hS0K2EbE8nV1nd96s0EF4c7R05WGjV39ztfhQtKrO7492nCLAOWg3gOdrW10B1pNlbVavf8SCSQ1McQFNDxlqauXTJWqDE8EUO2qaFUYVAgMBAAECggEBAJhZVCFISJWzQSsdcs9hZ5T3mxGGlNj+ohnW2WbvmQFXxq9H8gl2HLwxHhJYIHZ2b83+Xtwy7kGYV0BOY5CcqRTsE/KhqdA+wH+3RE2/YPuC/DfqWE9NvxjSidn2zDQby1GlPcrUGjGUipsV7388CSALwLva9YeL/eiDyMS63o2h7guiQLGcVEQ02Rq6pLEOVzZfyrEVMahKyCF5LAbEnqo0JLAJ3AhHisxYr/HM02cdXjAnom9rCsyyyx9hSEOLjyknlgIkxycw7kj/oVkpoRT+MD6BqAxvz5xdttJj4OTpwmggq9i480ABwxYU2IwKFPVzRU/eKdRa6iudlKh3owECgYEA0zkuJ1Wyxynp+SbHq7fb7tIUxzZuUzr/uI6y2pIM4MliEF+5exF37ZmvAs8MK4Zb0VqSW80QgWI73BNHXaQVj8E8w47/LWpsowJiDU0eELkEtySIHrc3cWujgp2Xay3aZF2UBL4xvwA1GS2Yv5fBsS3kw22/aitzbU2VJVsBEg0CgYEAvAWOKjCU3KHmKGwxT7zK53APn3NhwV6saEEMnQAtRbmXN8vMCESyRbJN1aSvS23LKTsPgisrqL35a10dGDpzRu/8/4+Go+4bHJ40Jr0oFB/pfNkuwEu8pEy3/cx7WQ1hL7D/REks4UrAywIkeTNVvYTBuV/Q4aAADPrmjD8waikCgYAP1ZyJJFqHpbO9ktOrIthg+ezG1mTE5opfGot76TD/9PjeA18TQ8b6z+eXzU+7P5JIioKoUSIooOWd5PAXrAJVyYW1n1Wv4OeN/NK+7oYPeN9Kn3l43AFTBl+2nfvI9CIIHh/Hjz6JdAmG/q0Rx92kW61nnSWZXvKDQiqpJz5YTQKBgCO08mC0xTB5VppKobrA4c++Cll/sgrlDdzmrZyLUg2xxzQDcgYH9csip4FxOxEaEsELxFA2Hp53JYzsLABT6VyJawSlny42o9N1f/Xzak8YGGFrZWODfpm9MBv031/0GIbUGEZBl1JZve9Pwtl5lEjfk2K+/Y47sobEh1APGASxAoGAdDxLPcDw6DJgkYWmw21QNzWeIhm++oatlIvV82RECoV4ju7OTCfOYTL4a8aRHBHhqqqUs9n2+2qP2yg0rOZ5hnMVdkPLEQMsQuQgoUV7UfT+VwDZNLMRO/H/PtW/RrfkWG9huufHuLDK4mXlaC9F8Hcm1UqjOxjyJKoEOqqyjNY=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi++affuNhDgCBT0YvOkpCdYttGeO/vHW/S3DGWgkkql8bc8a0ELVZx0n8Y5bA7u9F+C+8hYZAwcndlmD5ajtQkdx1riQ1/JXlHaJwhDnHoqm3UeTicqAHeGsGoqRfBKSbyzKOV+BVEqx7FZ+ltZ2siBf3gzyEil2U5D3vKHJLTFKx6p4lws0gly6gaH1qfDjeJtpGUDBSxbhhljHWv07K5IJ+2Z/AbHRvAOtCPZUJ3HUPjV3K0/RqFUCmha0GILeG0qiRyMiHe9OUKnHJJ4Y6QCcZHLV8Z7KTAY33me1vDMMOX35zLe9BXDI+Hu73NFR6MlAu8cwBMpCzjAQm7gGbQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="https://coyote-polite-macaque.ngrok-free.app/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private  String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
